<?php
$objXajax->registerFunction("fmfrmreportcurrent_load");
$objXajax->registerFunction("fmfrmreportcurrent_showBill");
$objXajax->registerFunction("fmfrmreportcurrent_setPhonenumber");
$objXajax->registerFunction("fmfrmreportcurrent_saveSMS");

function fmfrmreportcurrent_setPhonenumber(){
      global $lang,$objDbUpdate,$hotel_id;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $sql = 'select phone_auto_send_sms as phone from cus_config where service_id = '.$hotel_id;
    $arrPhone = $objDbUpdate->GetArray($sql);
    $phone = $arrPhone[0]['phone'];     
    $objResponse->addScript("$('#fmfrmreportcurrent_txtphonenumber').val('$phone'); "); 
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
}

function fmfrmreportcurrent_load($mhd=0){     

    global $lang,$objDbUpdate,$hotel_id;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    
    $arrData = fmfrmreportcurrent_createArray($total,$mhd);
    $count = count($arrData);
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_reportcurrent_grid.html');
    $oTbs->MergeBlock('blk_data', $arrData);
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('fmfrmreportcurrent_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('fmfrmreportcurrent_tbl',true);");
   
    if($mhd==0 || strlen($mhd)==0){        
        $money_get = 0;
        $money_pay =0;
        $sqlpay = "select sum(money) as money,flg from cus_pay where datediff(date_created,now())=0 and service_id = '$hotel_id' group by flg ";
        $arrPay = $objDbUpdate->GetArray($sqlpay);
        for($i=0;$i<count($arrPay);$i++){
            if($arrPay[$i]['flg']==1){
               $money_get = $arrPay[$i]['money']; 
            }else{
                $money_pay = $arrPay[$i]['money']; 
            }
        }
        $money_revenue = $total - $money_pay + $money_get;
    
        $datesend = date('d.m.Y');
        $timesend = date('H:i:s');
        $total = FormatMoney($total);
        $money_revenue = FormatMoney($money_revenue);
        $money_pay = FormatMoney($money_pay);
        $money_get = FormatMoney($money_get);
        $smscontent =  SMS_SYSTEM_MONEY_PRODUCT.$total. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_GET_OTHER.$money_get. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_PAY_OTHER.$money_pay.
         SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_DOANHTHU.$money_revenue. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_CAL_FROM.$timesend. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_DATE.$datesend; 
        $objResponse->addScript("fmfrmreportcurrent_smscontent='$smscontent';");
        $objResponse->addAssign('fmfrmreportcurrent_doanhthu', 'innerHTML', $total);
        $objResponse->addScript("$('#fmfrmreportcurrent_moneyget').html('$money_get'); "); 
        $objResponse->addScript("$('#fmfrmreportcurrent_moneypay').html('$money_pay'); "); 
        $objResponse->addScript("$('#fmfrmreportcurrent_revenue').html('$money_revenue'); ");
        $objResponse->addScript("$('#fmfrmreportcurrent_doanhthusms').html('$total'); ");
        $objResponse->addScript("$('#fmfrmreportcurrent_moneygetsms').html('$money_get'); "); 
        $objResponse->addScript("$('#fmfrmreportcurrent_moneypaysms').html('$money_pay'); "); 
        $objResponse->addScript("$('#fmfrmreportcurrent_revenuesms').html('$money_revenue'); ");  
        $objResponse->addScript("$('#fmfrmreportcurrent_timesms').html('$timesend'); "); 
        $objResponse->addScript("$('#fmfrmreportcurrent_datesms').html('$datesend'); ");
        
    }
    $objResponse->addScript('changeWaiting(1);');                 
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function fmfrmreportcurrent_createArray(&$total,$mhd){
    global $objDbUpdate,$tbl_postfix;
    $lens =  strlen($mhd); // chiều dài ban đâu mã đơn khi chưa biến thành integer;
    $mhd = (int)($mhd);
    $lent =  strlen($mhd); // chiều dài sau khi mã hóa đơn biến thành integer; 
    $condition ='';
    if($mhd>0){
        if($lens==$lent){
           $condition = " and c.general_id LIKE '%$mhd%'"; 
        }else{
           $condition = " and c.general_id LIKE '$mhd%'"; 
        }
        
    }
   $sql = "  SELECT LPAD(c.general_id,7,0) as mhd, c.promotion,sum(c.num*(c.price+c.area_extprice)) as total, c.product_id,c.date_update ,f.name ,c.general_id,c.promotion,rt.name as roomtype
        FROM cus_history_product$tbl_postfix c  left join fm_room f on c.table_id = f.id  left join fm_room_type rt on f.room_type = rt.id
      where c.flg=1  and datediff(c.date_update,now())=0  $condition
      group by c.general_id order by rt.id, c.general_id asc, f.name asc , c.date_update desc  ;"; 
    $arr = array();    
    try{
        $arr = $objDbUpdate->GetArray($sql);
        $count =  count($arr);
         $total = 0; 
        for($i=0;$i<$count;$i++){
            $temp = round($arr[$i]['total'],-3);
           //$total +=$temp; 
           $promo_value =round($temp*$arr[$i]['promotion']/100,-3); 
           $arr[$i]['promo_value']= $promo_value;            
           $arr[$i]['total']= $temp - $promo_value; 
           $total +=$temp - $promo_value;             
        }
       
    }
    catch(Exception $e){
        $arr = array();
    } 
    return  $arr  ;
}

function fmfrmreportcurrent_showBill($general_id){ 
    global $objDbSelect,$user_id,$tbl_postfix,$total,$lang,$roomname,$promo_value;
   $objResponse = new xajaxResponse();
   
    $sql="SELECT  promotion,name, sum(num) as num, sum((price + area_extprice) *  num) as total,0 as style FROM cus_history_product$tbl_postfix 
    where general_id = '$general_id' and num >0 group by product_id,area_extprice ";
    $arr = $objDbSelect->GetArray($sql);  
   $total =0; 
   
   $count = count($arr);     
    for($i=0;$i<$count;$i++){
        $total += (int)$arr[$i]['total']; 
        $total = round($total,-3);
    }
    $promo_value = 0;
    if($arr[0]['promotion']!=0){
        $promo_value =-round($total*$arr[0]['promotion']/100,-3);
        $total += $promo_value; 
    }
    
     
    $bill = 'fm_reportcurrent_bill.html';  
    $oTbs = new clsTinyButStrong();    
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/'.$bill);   
    $oTbs->MergeBlock('blk_list', $arr); 
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmreportcurrent_divprint', 'innerHTML', $oTbs->Source);
    if($promo_value!=0){
      $objResponse->addScript("$('#fmfrmreportcurrent_trPromotionBill').show()");   
    }else{
      $objResponse->addScript("$('#fmfrmreportcurrent_trPromotionBill').hide()");  
    }    
    $objResponse->addScript("$('#fmfrmreportcurrent_divprint').dialog('open')");     
    
    return $objResponse->getXML();
}

function fmfrmreportcurrent_saveSMS($phonenumber,$fmfrmreportcurrent_smscontent){
     global $objDbSelect,$user_id,$hotel_id;
     $objResponse = new xajaxResponse(); 
     $arrPhone = explode(';',$phonenumber);
     $num_phone = count($arrPhone);
     $sql = "INSERT INTO tbl_sms(date_created, sms_type, sms_content, service_id, phone_number, num_phone, user_created, sms_timesend)
     values(now(),1,'$fmfrmreportcurrent_smscontent',$hotel_id,'$phonenumber',$num_phone,$user_id,now())" ;
     if($objDbSelect->Execute($sql)){
       $objResponse->addScript("$('#fmfrmreportcurrent_divsms').dialog('close');");     
       $objResponse->addAlert("Bạn đã gửi tin nhắn thành công");     
     }     
    
     return $objResponse->getXML(); 
}

# end region process data
?>
